import 'package:cached_network_image/cached_network_image.dart';
import 'package:changxindianzishangwu/model/address_book_model.dart';
import 'package:changxindianzishangwu/pages/contacts/conversation_friend_page.dart';
import 'package:changxindianzishangwu/pages/contacts/search_user_page.dart';
import 'package:changxindianzishangwu/pages/find/report_page.dart';
import 'package:changxindianzishangwu/pages/home_page.dart';
import 'package:changxindianzishangwu/provider/currentMessageList.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_callback.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_conversation.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_message.dart';
import 'package:tencent_im_sdk_plugin/tencent_im_sdk_plugin.dart';

class FriendInfoSettingPage extends StatefulWidget {
  V2TimConversation conversation;
  List<V2TimMessage> messages;

  FriendInfoSettingPage({Key key, this.conversation, this.messages})
      : super(key: key);

  @override
  _FriendInfoSettingPageState createState() => _FriendInfoSettingPageState();
}

class _FriendInfoSettingPageState extends State<FriendInfoSettingPage> {
  bool _lights = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.conversation != null) {
      if (widget.conversation.isPinned == null) {
        widget.conversation.isPinned = false;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text(
          "好友设置",
          style: TextStyle(fontSize: ScreenUtil().setSp(16)),
        ),
        centerTitle: true,
      ),
      body: Column(
        children: [
          Container(
            color: Colors.white,
            height: 100,
            child: Column(
              children: [
                Row(
                  children: <Widget>[
                    InkWell(
                      onTap: () {
                        Get.to(() => ConversationFriendPage(
                              type: 4,
                              model: AddressBookModel(
                                name: widget.conversation.showName,
                                userId: widget.conversation.userID,
                                headerUrl: widget.conversation.faceUrl,
                              ),
                            ));
                      },
                      child: Container(
                        height: 44.w,
                        width: 44.w,
                        margin: EdgeInsets.only(left: 15.w),
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(6),
                          child: CachedNetworkImage(
                            imageUrl: widget.conversation.faceUrl,
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                    ),
                    // Container(
                    //   height: 44.w,
                    //   width: 44.w,
                    //   margin: EdgeInsets.only(left: 15.w),
                    //   decoration: BoxDecoration(
                    //       borderRadius: BorderRadius.circular(6),
                    //       border: Border.all(
                    //           width: 1,
                    //           color: ZdColor.rgba(153, 153, 153, 1),
                    //           style: BorderStyle.solid)),
                    //   child: Icon(
                    //     Icons.add,
                    //     color: ZdColor.rgba(153, 153, 153, 1),
                    //   ),
                    // )
                  ],
                ),
                Row(
                  children: [
                    Container(
                        height: 22.w,
                        alignment: Alignment.center,
                        margin: EdgeInsets.only(left: 15.w),
                        child: Text(
                          widget.conversation.showName,
                          style: TextStyle(fontSize: 12.sp),
                        )),
                  ],
                ),
              ],
            ),
          ),
          Container(
            alignment: Alignment.center,
            color: Colors.white,
            margin: EdgeInsets.only(top: 10),
            child: Column(
              children: <Widget>[
                Divider(
                  height: 1,
                  indent: ScreenUtil().setWidth(10),
                  color: Color.fromRGBO(248, 248, 248, 1),
                  thickness: ScreenUtil().setHeight(1),
                ),
                MergeSemantics(
                  child: ListTile(
                    title: Text(
                      '消息置顶',
                      style: TextStyle(fontSize: ScreenUtil().setSp(15)),
                    ),
                    trailing: CupertinoSwitch(
                      activeColor: Color.fromRGBO(0, 220, 169, 1),
                      value: widget.conversation.isPinned ?? false,
                      onChanged: (bool value) async {
                        setState(() {
                          widget.conversation.isPinned =
                              !widget.conversation.isPinned;
                        });

                        print("comming!");
                        if (widget.conversation.isPinned ?? false) {
                          TencentImSDKPlugin.v2TIMManager.v2ConversationManager
                              .pinConversation(
                                  conversationID:
                                      widget.conversation.conversationID,
                                  isPinned: widget.conversation.isPinned)
                              .then((value) {
                            if (value.code == 0) {
                              showToast("顶置成功");
                            }
                          });
                          print("comming!");
                        } else {
                          TencentImSDKPlugin.v2TIMManager.v2ConversationManager
                              .pinConversation(
                                  conversationID:
                                      widget.conversation.conversationID,
                                  isPinned: widget.conversation.isPinned)
                              .then((value) {
                            if (value.code == 0) {
                              showToast("取消置顶成功");
                            }
                          });
                        }
                      },
                    ),
                    onTap: () async {},
                  ),
                ),
              ],
            ),
          ),
          Container(
            alignment: Alignment.center,
            color: Colors.white,
            margin: EdgeInsets.only(top: 10),
            child: Column(
              children: <Widget>[
                InkWell(
                  onTap: () {
                    //todo头像跳转
                    // Get.to(() => AddSettingPage());
                    Get.to(() => SearchUserPage());
                  },
                  child: ListTile(
                    leading: Text(
                      "查找聊天内容",
                      style: TextStyle(fontSize: ScreenUtil().setSp(15)),
                    ),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),
                ),
                Divider(
                  height: 1,
                  indent: ScreenUtil().setWidth(10),
                  color: Color.fromRGBO(248, 248, 248, 1),
                  thickness: ScreenUtil().setHeight(1),
                ),
                InkWell(
                  onTap: () {
                    //todo头像跳转
                    // Get.to(() => AddSettingPage());
                    _openModalBottomSheet();
                  },
                  child: ListTile(
                    leading: Text(
                      "清空聊天记录",
                      style: TextStyle(fontSize: ScreenUtil().setSp(15)),
                    ),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),
                ),
              ],
            ),
          ),
          Container(
            alignment: Alignment.center,
            color: Colors.white,
            margin: EdgeInsets.only(top: ScreenUtil().setHeight(10)),
            child: InkWell(
              onTap: () {
                Get.to(ReportPage(1, widget.conversation.userID));

                //todo头像跳转
                // Get.to(() => AddSettingPage());
              },
              child: ListTile(
                leading: Text(
                  "投诉/举报",
                  style: TextStyle(fontSize: ScreenUtil().setSp(15)),
                ),
                trailing: Icon(Icons.keyboard_arrow_right),
              ),
            ),
          )
        ],
      ),
    );
  }

  Future _openModalBottomSheet() async {
    print(1);
    final option = await showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return Container(
            height: 200.0,
            child: Column(
              children: <Widget>[
                ListTile(
                  title: Text(
                    '清除聊天记录',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontSize: 13.sp, color: ZdColor.rgba(143, 143, 143, 1)),
                  ),
                  onTap: () {
                    //_photoSelect();
                    Navigator.pop(context, '清除聊天记录');
                  },
                ),
                Divider(
                  height: 1,
                  indent: ScreenUtil().setWidth(10),
                  color: Color.fromRGBO(248, 248, 248, 1),
                  thickness: ScreenUtil().setHeight(1),
                ),
                ListTile(
                  title: Text(
                    '清空聊天记录',
                    textAlign: TextAlign.center,
                    style: TextStyle(fontSize: 20.sp, color: HiColor.red),
                  ),
                  onTap: () {
                    // _imagesSelect();
                    widget.messages.forEach((element) {
                      print(element.msgID);
                      deleteMessageFromLocalStorage(element.msgID);
                    });
                  },
                ),
                Divider(
                  height: 1,
                  indent: ScreenUtil().setWidth(10),
                  color: Color.fromRGBO(248, 248, 248, 1),
                  thickness: ScreenUtil().setHeight(1),
                ),
                ListTile(
                  title: Text('取消', textAlign: TextAlign.center),
                  onTap: () {
                    Navigator.pop(context, '取消');
                  },
                ),
              ],
            ),
          );
        });
    print(option);
  }

  void deleteMessageFromLocalStorage(String msgId) async {
    V2TimCallback res = await TencentImSDKPlugin.v2TIMManager
        .getMessageManager()
        .deleteMessageFromLocalStorage(msgID: msgId);
    if (res.code == 0) {
      showToast("删除成功");
      Navigator.pop(context, '清空聊天记录');
      Provider.of<CurrentMessageListModel>(context, listen: false)
          .deleteMessage(widget.conversation.conversationID);
      Get.offAll(HomePage());
    }
    print(res.toJson());
  }
}
